// weapp/fields_editor/table_list/table_list.js
var gb = getApp().globalData
var server =gb.server
var cfg = gb.cfg

Component({
    /**
     * 组件的属性列表
     */
    properties: {
        head:{
            type:Object,
        },
        value:{
            type:String,
            value:'[]'
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        rows:[
            // {value:1,count:2,price:100},
            // {value:2,count:1,price:100},
            // {value:1,count:2,price:100},
        ]
    },

    observers:{
        rows(nv){
            var string = JSON.stringify(this.data.rows)
            if(string!=this.properties.value){
                this.triggerEvent('input',string )
            }
            
        },
        value(nv){
        
            if(JSON.stringify(this.data.rows) != this.properties.value){
                if(this.properties.value==""){
                    var rows =[]
                }else{
                    var rows = JSON.parse(this.properties.value)
                }
                this.setData({
                    rows:rows
                })
            
            }

            
        }
      },
    /**
     * 组件的方法列表
     */
    methods: {
        addNew(){
            var self = this
            wx.navigateTo({
              url: '../../weapp/fields/fields',
              success: function(res) {
                // 通过eventChannel向被打开页面传送数据
                res.eventChannel.emit('fields_ctx', {
                  row:{},
                  groups:[
                    {label:'编辑',heads:self.properties.head.fields_heads}
                  ],
                  actions:[
                    {label:'确定',name:'submit',onClick:async (row)=>{
                        if(self.properties.head.validate_fun){
                            var error = await self.properties.head.validate_fun(row)
                            if(error){
                                cfg.toast(error)
                                return
                            }
                        }

                        var origin_rows = self.data.rows
                        origin_rows.push(row)
                        self.setData({
                            rows:origin_rows
                        })
                        wx.navigateBack({})
                    }
                    },
                  ],
                  after_input_fun:self.properties.head.after_input_fun
                 })
              }
            })
        },
        onRowClick(e){
            var index = e.detail
            var row_in = this.data.rows[index]
            var self = this
            wx.navigateTo({
              url: '../../weapp/fields/fields',
              success: function(res) {
                // 通过eventChannel向被打开页面传送数据
                res.eventChannel.emit('fields_ctx', {
                  row:row_in,
                  groups:[
                    {label:'编辑',heads:self.properties.head.fields_heads}
                  ],
                  actions:[
                    {label:'确定',name:'submit',onClick: async (row)=>{
                        if(self.properties.head.validate_fun){
                           var error =  await self.properties.head.validate_fun(row)
                           if(error){
                                cfg.toast(error)
                                return
                            }
                        }
                        
                        var origin_rows = self.data.rows
                        origin_rows[index] = row
                        self.setData({
                            rows:origin_rows
                        })
                        wx.navigateBack({})
                    }
                    },
                  ],
                  after_input_fun:self.properties.head.after_input_fun
                 })
              }
            })


        },
        onRowDelete(e){
            var index = e.detail
            var origin_rows = this.data.rows
            origin_rows.splice(index,1)
            this.setData({
                rows:origin_rows
            })
        }
        // modifyItem(e){
        //     var row_in = e.currentTarget.dataset.row
        //     var self = this
        //     wx.navigateTo({
        //       url: '../../weapp/fields/fields',
        //       success: function(res) {
        //         // 通过eventChannel向被打开页面传送数据
        //         res.eventChannel.emit('fields_ctx', {
        //           row:row_in,
        //           groups:[
        //             {label:'编辑',heads:[
        //               {name:'label',label:'地址'},
        //             ]}
        //           ],
        //           actions:[
        //             {label:'确定',name:'submit',onClick:(row)=>{
        //               row_in = row
        //               wx.navigateBack({})

        //             }
        //             },
        //           ],
        //          })
        //       }
        //     })
        // }
    }
})
